# -*- coding: utf-8 -*-
import scrapy
import re
from urllib import parse
from scrapy.http import Request
from giant.items import GiantComicItem


class ComicSpider(scrapy.Spider):
    name = 'comic'
    allowed_domains = ['http://manhua.fzdm.com']
    start_urls = ['http://manhua.fzdm.com/39/']
    base_img_domain = 'http://p1.xiaoshidi.net'

    def parse(self, response):
        print(self.start_urls)
        # 获取漫画章节列表
        lis = response.css('.pure-u-1-2.pure-u-lg-1-4')
        for li in lis:
            url = li.css('a::attr(href)').extract_first('')
            if url:
                url = parse.urljoin(response.url, url)
                print(url)
                # 章节url
                yield Request(
                    url=url,
                    callback=self.parse_chapter,
                    dont_filter=True
                )

    def parse_chapter(self, response):
        """
        获取每一个章节的详细
        :param response: 
        :return: 
        """

        if 'index' not in response.url:
            # 将第一页丢入　parse_item
            yield Request(
                url=response.url,
                callback=self.parse_item,
                dont_filter=True
            )

        print('====== 抓取 {}'.format(response.url))
        re_match = re.match(".*<a href='(.*?)' id=\"mhona\">下一页</a></div>", response.text.replace('\n', ''))
        if re_match and re_match.group(1):
            next_page_url = parse.urljoin(response.url, str(re_match.group(1)))
            yield Request(
                url=next_page_url,
                callback=self.parse_chapter,
                dont_filter=True
            )

    def parse_item(self, response):
        path = re.match('.*var mhurl = "(.*?)";', response.text.replace('\n', ''))
        if path and path.group(1):
            image_url = parse.urljoin(self.base_img_domain, str(path.group(1)))
            giant_comic_item = GiantComicItem()
            giant_comic_item['image_url'] = [image_url]
            print('====== 图片 {}'.format(image_url))
            yield giant_comic_item
